#/bin/bash

declare -a Ap_Mac

Mysql="mysql -udev -pdev -h10.64.2.206"

#ruckesap
Check_ruckes() {
	Jsession=`curl --connect-timeout 4 -i  -s -k 'https://10.64.1.241:8443/wsg/api/scg/session/login '  -X PUT  -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"userName":"admin","password":"Windfind123#@!"}' --compressed | awk -F " |;" /JSESSIONID/'{print $2}'`
	curl --connect-timeout 4 -s 'https://10.64.1.241:8443/wsg/api/scg/apsE/byZone/f77a8816-3049-40cd-8484-82919275ddc3/exportcsv?searchType=and'  -H "Cookie: ${Jsession}" --compressed --insecure | awk -F',' '/Disconnect/{print $1}' | awk -F'"' '{print $2}'
}

#经信委
Check_ruckes_jxw() {
	Jsession_jxw=`curl --connect-timeout 4 -i -s 'https://10.65.0.253/admin/login.jsp'  --data 'url=undefined&username=admin&password=Windfind123#@!&ok=Log+In' --compressed --insecure | awk -F " |;" '/-ejs-sessio/{print $2}'`
	curl --connect-timeout 4 -s 'https://10.65.0.253/admin/_saveexportcsv.jsp?action=exportaplist&filename=071018_14_36&searchinfo=' -H "Cookie: ${Jsession_jxw}" --compressed --insecure | awk -F',' '/Disconnect/{print $1}' | awk '$1=toupper($1)'
}

#h3c
Check_H3C() {
	declare -a DEVs
	#declare -a h3c_mac
	KEy="apikey:3c17e02e8e974503bd9ab6ed28378885"
	DEVs=("219801A0WJC173000003" "219801A0WJC173000025")
	for devs in ${DEVs[@]}; do
		#curl -s "https://lvzhouapi.h3c.com/device/apinfo?devsn=${devs}" -H "${KEy}" --compressed --insecure | grep -oP "apSN.*\}" | sed s/'},{'/"\n"/g | awk -F, '{print $2,$NF}' | awk -F'"' '{print $8}' |awk '$1=toupper($1)' | sed -r 's/(\w{2})(\w{2})/\1-\2/g' | sed -e 's/-/:/g'
		curl --connect-timeout 4 -s "https://lvzhouapi.h3c.com/device/apinfo?devsn=${devs}" -H "${KEy}" --compressed --insecure | grep -oP "apSN.*\}" | sed s/'},{'/"\n"/g | awk -F, '/"apStatus":0/{print $2,$NF}' | awk -F'"' '{print $8}' |awk '$1=toupper($1)' | sed -r 's/(\w{2})(\w{2})/\1-\2/g' | sed -e 's/-/:/g'

	done
}

#环创
Check_Hc() {
	declare -a Route_list
	declare -a offline_list
	Route_list=`${Mysql} -s -N -e "SELECT radiusLocation FROM Light_Manager.location where portalLocation like 'HC-%';"`
	for route_id in ${Route_list[@]}; do
		#Result=`curl -s "http://www.login-wifi.com/cmps/admin.php?m=Reportapiuser&a=isOnline&user=gzfxkj&password=Windfindtech123%23%40%21&gw_id=${route_id}" --compressed  | awk -F'{|:|"' '{print $11}'`
		Result=`curl --connect-timeout 4 -s "http://www.login-wifi.com/cmps/admin.php?m=Reportapiuser&a=isOnline&user=gzfxkj&password=密码呀&gw_id=${route_id}" --compressed  | awk -F'{|:|"' '{print $11}'`
		if [[ ${Result} != "online" ]]; then
			offline_list[${#offline_list[*]}]=${route_id}
		fi
	done
	sql_list=`echo ${offline_list[@]} | sed s/" "/"','"/g`
	${Mysql} -s -N -e "SELECT mac FROM Light_Manager.location l left join Light_Manager.ap a on l.id = a.locationID where radiusLocation in('${sql_list}');"
}

ruckes_ap=(`Check_ruckes`)
ruckes_jxw=(`Check_ruckes_jxw`)
H3C_ap=(`Check_H3C`)
Hc_ap=(`Check_Hc`)

#Ap_Mac=`Check_ruckes;Check_ruckes_jxw;Check_H3C;Check_Hc`
Ap_Mac=(${ruckes_ap[@]} ${ruckes_jxw[@]} ${H3C_ap[@]} ${Hc_ap[@]})
# echo ${Ap_Mac[@]}

mac_list=`echo ${Ap_Mac[@]} | sed -r "s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}) /\1',\'/g"`
# echo  ${mac_list}

#开始写文件
Mail_file="/tmp/list.html"
echo -e "不在线ap一共有`echo ${mac_list} | awk -F',' '{print NF}'`个<br />" > ${Mail_file}
echo -e "云AC:${#ruckes_ap[@]}个<br>" >> ${Mail_file}
echo -e "经信委大楼:${#ruckes_jxw[@]}个<br>" >> ${Mail_file}
echo -e "残联设备:${#H3C_ap[@]}个<br>" >> ${Mail_file}
echo -e "环创设备:${#Hc_ap[@]}个<br>" >> ${Mail_file}

echo -n "<table border="1">" >> ${Mail_file}
# ${Mysql} -e "SELECT l.name as 场点名称,a.name as Ap名称,a.mac as Apmac,a.installPosition as 安装位置 FROM Light_Manager.ap a LEFT JOIN Light_Manager.location l ON a.locationID = l.id WHERE a.mac IN ('${mac_list[@]}');"  | awk -v OFS="," '{print "<tr><td>"$1"</td>,<td>"$2"</td>,<td>"$3"</td>,<td>"$4"</td></tr>"}' >> ${Mail_file} 
${Mysql} -e "SELECT l.name as 场点名称,a.name as Ap名称,a.mac as Apmac,a.installPosition as 安装位置 FROM Light_Manager.ap a LEFT JOIN Light_Manager.location l ON a.locationID = l.id WHERE a.mac IN ('${mac_list[@]}');"  | awk '{print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td></tr>"}' >> ${Mail_file} 
echo -n "</table>" >> ${Mail_file}

#发邮件
cat ${Mail_file} | mutt -s "当前状态异常ap列表"  -e 'set content_type="text/html"' jc.wu@windfindtech.com jianrong.wang@windfindtech.com zhize.wang@juneyaokc.com wenqi.li@windfindtech.com




#echo ${mac_list}





#mysql -uroot -p1234-qwer -h10.64.2.206 -e "call Light_Manager.netSecurity_heartbeat(${mac_list});"






# Ap_Mac_ruckes=`Check_ruckes`
# Ap_Mac_jxw=`Check_ruckes_jxw`
# Ap_Mac_H3C=`Check_H3C`
# Ap_Mac_HC=`Check_Hc`



# echo ${Ap_Mac_ruckes[@]} | sed -r "s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}) /\1,/g" | sed -r "s/(.*)/'\1'/g" | sed "s/://g"
# echo ${Ap_Mac_jxw[@]} | sed -r "s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}) /\1,/g" | sed -r "s/(.*)/'\1'/g" | sed "s/://g"
# echo ${Ap_Mac_H3C[@]} | sed -r "s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}) /\1,/g" | sed -r "s/(.*)/'\1'/g" | sed "s/://g"
# echo ${Ap_Mac_HC[@]} | sed -r "s/(\w{2}:\w{2}:\w{2}:\w{2}:\w{2}:\w{2}) /\1,/g" | sed -r "s/(.*)/'\1'/g" | sed "s/://g"


